﻿using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using System.Text;
using Texfina.Core.Common;
using Texfina.Common.Mg;
using Texfina.Core.Web;
using Texfina.Core.Data;
using Texfina.Entity.Sy;
using Texfina.Entity.Rh;
using Texfina.Entity.Mg;
using SYBOQry = Texfina.BOQry.Sy;
using RHBOQry = Texfina.BOQry.Rh;
using MGBOQry = Texfina.BOQry.Mg;
using Newtonsoft.Json;

namespace SY
{
   public partial class wfUsuarioBnd : SearchPage
   {
      public const int _GRDLIST_STANULADO = 6;

      protected void Page_Load(object sender, EventArgs e)
      {

         this.LoadInitialSettings();
         if (this.IsPostBack)
         {
            this.LoadInPostBack();
            return;
         }

         this.LoadData();
         SearchData();
      }

      public override void LoadInitialSettings()
      {
         Label lbl = (Label)this.Master.FindControl("lblTitle");
         lbl.Text = "Usuarios";
         this.Page.Form.DefaultButton = bttSearch.UniqueID;
      }

      public override void LoadListControls()
      {
         //FOR FILTER
         if (Request.QueryString["Filter"] == null)
            Session[ConstSessionVar.UFILTER] = "";
         else
            hdnFilter.Value = Session[ConstSessionVar.UFILTER].ToString();
         //END FOR FILTER
      }

      //public override void RegisterGeneralScripts()
      //{
      //   StringBuilder sb = new StringBuilder();

      //   sb.AppendLine("var ctl = document.getElementById('" + txtidLogon.ClientID + "');");
      //   sb.AppendLine("ctl.value='';");
      //   sb.AppendLine("ctl = document.getElementById('" + txtdsNombres.ClientID + "');");
      //   sb.AppendLine("ctl.value='';");
      //   sb.AppendLine("return false;");

      //   bttClear.Attributes.Add("onclick", sb.ToString());

      //   sb = new StringBuilder();
      //   sb.AppendLine(bttSearch.ClientID + ".disabled=true;");
      //   sb.AppendLine(this.ClientScript.GetPostBackEventReference(bttSearch, "") + ";");

      //   bttSearch.Attributes.Add("onclick", sb.ToString());

      //}

      public override void SearchData()
      {

         EUsuario objE = new EUsuario();

         //FOR FILTER
         DataTable dtToken = (DataTable)JsonConvert.DeserializeObject(hdnFilter.Value, (typeof(DataTable)));
         Session[ConstSessionVar.UFILTER] = hdnFilter.Value;
         textFilter.DataSourceListToken = dtToken;
         objE.Filter = WebHelper.GetFilterWhere(dtToken, "id");
         //objE.Filter = WebHelper.AddFilterWhere(objE.Filter, "a.idLogon", OperatorWhere.igual, Session[ConstSessionVar.EMPRESA].ToString(), LogicalWhere.AND);
         objE.Filter = WebHelper.AddFilterWhere(objE.Filter, "a.stAnulado", OperatorWhere.igual, ('0').ToString(), LogicalWhere.AND);
         //FOR FILTER

         //if (this.txtidLogon.Text == "")
         //   objE.IdLogon = null;
         //else
         //   objE.IdLogon = this.txtidLogon.Text;

         //if (this.txtdsNombres.Text == "")
         //   objE.DsNombres = null;
         //else
         //   objE.DsNombres = this.txtdsNombres.Text;

         IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(SYBOQry.Usuario));
         DataTable dt = objBO.GetByCriteria(objE);

         this.GridSetDataBinding(grdList, dt);

      }

      protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
      {
         String strQS = "";

         switch (e.Item.Value)
         {
            case ConstMenuToolbarValue.BUTTON_NEW:
               strQS = "wfUsuarioMnt.aspx?formEA=" + Convert.ToInt32(FormEditAction.AddNew).ToString() + "&idUser=";
               break;
         }
         //FOR FILTER
         Session[ConstSessionVar.UFILTER] = hdnFilter.Value;
         //END FOR FILTER
         Response.Redirect(strQS);
      }

      protected void grdList_RowCommand(Object sender, GridViewCommandEventArgs e)
      {
         DataRow dr = this.GridGetRowFromDataKey(grdList, Convert.ToInt32(e.CommandArgument));
         FormEditAction formEA = FormEditAction.AddNew;

         switch (e.CommandName)
         {
            case ConstGridRowCommand.GRIDEDIT:
               formEA = FormEditAction.Edit;
               break;
            case ConstGridRowCommand.GRIDDELETE:
               formEA = FormEditAction.Delete;
               break;
         }
         //FOR FILTER
         Session[ConstSessionVar.UFILTER] = hdnFilter.Value;
         //END FOR FILTER
         string strQS = "wfUsuarioMnt.aspx?formEA=" + Convert.ToInt32(formEA).ToString() + "&idUser=" + dr["idUser"].ToString();
         Response.Redirect(strQS);

      }

      //FOR FILTER
      protected void grdList_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
         if (e.Row.RowType == DataControlRowType.Header)
         {
            var CtrlFilterPanel = (esdFilterPanel)e.Row.FindControl("dsNombres");
            if (CtrlFilterPanel != null)
            {
               ETablaGenDet objEDet = new ETablaGenDet();
               MGBOQry.ITablaGen objBOI = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));

               objEDet.IdTabla = ConstMultitabla.TIPO_CONCEPTO_PLANILLA;
               DataTable dt = objBOI.GetDetailByList(objEDet);
               CtrlFilterPanel.DataSourceCombo = dt;
            }

         }

      }
      //END FOR FILTER

      protected void bttOk_Click(object sender, EventArgs e)
      {
         this.SearchData();
      }
   }
}